// pages/home/submit/index.js
const app = getApp()
const { baseURL } = require('../../../fetch/http')
const api = require('../../../fetch/api')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    total: 0, // 总计
    num: 0 , // 购买数量,
    gid:false, //特殊商品
    show: false,
    title:"微信支付",
    statuslist:["送货上门","自提物品"],
    current:0,
    zitiaddress:"",
    zitides:"",
    zititime:"",
    address:'',
    actions: [
      {
        name: '微信支付',
      },
      {
        name: '余额支付',
      }
    ],
  },  
  clklist(e){
   
 var index=e.currentTarget.dataset.index;
 this.setData({
   current:index
 })
  },
  payStatus(){
    this.setData({
      show:true
    })
  },
  onSelect(e){
    var title=e.detail.name;
    this.setData({
      title:title,
      show:false
    })
  },
  async pay() {
   
    if(this.data.current){
    }else{
      if(!this.data.address.id){
        wx.showToast({
          title: '请添加收货地址',
          icon:"none"
        })
        return
      }
    }
 
    if(this.data.gid){
      var res = await api.onlyProundct({
        address_id:this.data.current?'':this.data.address.id,
        goods_id: this.data.id,
        num: this.data.num,
        pay_type:this.data.title=='微信支付'?'1':'2'
      })
    }else{
      var res = await api.onlyProundct({
        address_id:this.data.current?'':this.data.address.id,
        goods_spec_id: this.data.id,
        num: this.data.num,
        pay_type:this.data.title=='微信支付'?'1':'2'
      })
    }
   
    api.wxpay(res.data).then(res => {
      if (res.errMsg == "requestPayment:ok") {
        app.toast('支付成功','success',()=>{
          wx.navigateTo({
            url: '/pages/my/orders/index',
          })
        })
      }
    }).catch(error => {
      console.log(error);
    })
  },
  
  reduce() {
    let { num } = this.data
    num > 1 && num--
    this.setData({
      num
    })
  },
  add() {
    let { num, spec } = this.data
    num < spec.sku && num++
    this.setData({
      num
    })
  },
  chooseAddress() {
    wx.navigateTo({
      url: '/pages/my/address/index?flag=true',
    })
  },
  async address() {
    let res = await api.address()
    let arr = res.data
    if(arr.length>0) {
      this.setData({
        address: arr[0]
      })
    }
  },
  async userinfo() {
    let res = await api.userinfo()
    console.log(res);
  },
  async submit() {
    if(this.data.gid){
      var res = await api.submit({
        goods_id: this.data.id,
        num: this.data.num
      })
    }else{
      var res = await api.submit({
        goods_spec_id: this.data.id,
        num: this.data.num
      })
    }
    
    let {goods,expressFee,payMoney,saleMoney,totalMoney} = res.data
    this.setData({
      goods,expressFee,payMoney,saleMoney,totalMoney
    })
  },
  async setting(){
    var res = await api.setting()
    if(res.code==200){
      var {takeAddress,takeRemark,takeTime}=res.data;
      this.setData({
        zitiaddress:takeAddress,
        zitides:takeRemark,
        zititime:takeTime
      })
    
    }
    console.log(res)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.gid){
      this.setData({
        id: options.gid,
        gid: true,     
        num: 1
      })
    }else{
      this.setData({
        id: options.id,
        num: options.num
      })
    }
   
    this.submit()
    this.setting()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let { address } = app.globalData
    if(address.province){
      this.setData({
        address
      })
    }else {
      this.address()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // }
})